#ifndef _MISSIONS_H_
#define _MISSIONS_H_


#include "Common.h"
#include "String.h"
#include "SharedPtr.h"
#include "Resource.h"
#include "Vector3.h"
#include "MissionModels.h"
#include "tinyxml/tinyxml.h"
#include "XMLData.h"


class MissionPtr;
class CreatureAIScript;


//任务公共属性
typedef struct mission_common_property
{

	int32	data1;
	int32	data2;
	int32	data3;
	int32	data4;

	float	fdata1;
	float 	fdata2;

	ResourceProxy		uplink;
	void initialize(void)
	{
		data1 = 0;
		data2 = 0;
		data3 = 0;
		data4 = 0;
		
		fdata1 = 0;
		fdata2 = 0;

		uplink.setNull();
	}

	mission_common_property()
	{
		initialize();
	}

}mission_common_property_t;

class Mission :public Resource
{
public:
	enum MissionStatus
	{
		MissionStatusNone		= 0,	//无效任务类型
		MissionStatusCreate		= 1,	//任务的创建
		MissionStatusProcess		= 2,	//任务处理中
		MissionStatusSucceed		= 3,	//任务成功
		MissionStatusFaild		= 4,	//任务失败
		MissionStatusCancle		= 5,	//任务取消
		MissionStatusClose		= 6,	//任务结束
		MissionStatusDeleted		= 255	//任务数据删除
	};

	enum MissionFlag
	{
		MissionFlagNone			= 0x00,		//无效类型
		MissionFlagTrigger		= 0x01,		//任务事件触发
		MissionFlagFixed		= 0x02,		//任务主动影响
		MissionFlagParnet		= 0x04,		//父级任务，用于任务分发
		MissionFlagFreeze		= 0x08,		//任务冻结
		MissionFlagSender		= 0x10,		//任务发送方
		MissionFlagRecver		= 0x20		//任务接收方
	};

	Mission(ResourceManager* creator, const String& name, ResourceHandle handle,
            const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
	~Mission(void);

protected:
       	void prepareImpl(void);
        /** Destroys data cached by prepareImpl.
         */
        void unprepareImpl(void);
        /// @copydoc Resource::loadImpl
        bool loadImpl(void);
        /// @copydoc Resource::postLoadImpl
        void postLoadImpl(void);

	 void preUnloadImpl(void);
        /// @copydoc Resource::unloadImpl
        void unloadImpl(void);
        /// @copydoc Resource::calculateSize
        size_t calculateSize(void) const;

private:
	uint32		m_serial;
	uint32		m_group_id;

	void    initialize(void);
public:

	const uint32  & getSerial() 	{	return m_serial;	}
	const uint32  & getGroup_id()	{	return m_group_id;	}

	const String & getMissionName(void);

	//save item to database	
	bool	SaveDB(void);
	bool	LoadDB(void);
	bool    FetchDB(Field * fields);

	MissionStatus getMissionStatus(void);
	void	setMissionStatus(const MissionStatus & status);

	void	Delete(void);
	void	Destroy(void);


	//任务条件
	XMLDataSet	require;
	//任务奖励
	XMLDataSet	bounty;
	//任务已完成
	XMLDataSet	done;


	//任务的模式属性
	MissionModelPtr	model;

	
	//任务公共属性
	mission_common_property_t	common;
};

    /** Specialisation of SharedPtr to allow SharedPtr to be assigned to MissionPtr 
    @note Has to be a subclass since we need operator=.
    We could templatise this instead of repeating per Resource subclass, 
    except to do so requires a form VC6 does not support i.e.
    ResourceSubclassPtr<T> : public SharedPtr<T>
    */
    class SERVER_DECL  MissionPtr : public SharedPtr<Mission>
    {
    public:
        MissionPtr() : SharedPtr<Mission>() {}
        explicit MissionPtr(Mission* rep) : SharedPtr<Mission>(rep) {}
        MissionPtr(const MissionPtr& r) : SharedPtr<Mission>(r) {}
        MissionPtr(const ResourcePtr& r);
        /// Operator used to convert a ResourcePtr to a MeshPtr
        MissionPtr& operator=(const ResourcePtr& r);
    protected:
        /// Override destroy since we need to delete Mesh after fully defined
        void destroy(void);
    };
	

#endif
